//
//  QShowChooseView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/11/23.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QShowChooseView.h"
#import "QShowSubChooseView.h"
@implementation QShowChooseView{

    UIControl *_maskView;
    QShowSubChooseView *_subView;
    
    CGFloat W;
    CGFloat H;

}

- (instancetype)initWithImage:(UIImage *)image
                      content:(NSString *)content
                    leftTitle:(NSString *)leftTitle
                   rightTitle:(NSString *)rightTitle
                         type:(NSInteger)type{
    
    self = [super init];
    if(self){
        W = 260;
        H = 240;
        _img = image;
        _content = content;
        _leftTitle = leftTitle;
        _rightTitle = rightTitle;
        _type = type;
        self.frame = [UIScreen mainScreen].bounds;
        [self createUI];
    }
    return self;
}

- (void)createUI{
    __block QShowChooseView *weakSelf = self;
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    _maskView.hidden = YES;
    [self addSubview:_maskView];
    
    _subView = [[[NSBundle mainBundle] loadNibNamed:@"QShowSubChooseView" owner:self options:nil] lastObject];
    _subView.img = _img;
    _subView.content = _content;
    _subView.leftTitle = _leftTitle;
    _subView.rightTitle = _rightTitle;
    _subView.type = _type;
    _subView.frame = CGRectMake((kScreenWidth-W)/2, kScreenHeight, W,H);
    [_maskView addSubview:_subView];
    _subView.block = ^(NSInteger tag){
        [weakSelf hidePlayBoxView];
        if(weakSelf.block){
            weakSelf.block(tag);
        }
    };
}

- (void)showPlayBoxView{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    [window addSubview:_maskView];

//    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:_maskView];
    _maskView.hidden = NO;
    _maskView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _subView.center = self.center;
        _maskView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidePlayBoxView{
    _maskView.alpha = 1;
    [UIView animateWithDuration:.3 animations:^{
        _subView.frame = CGRectMake((kScreenWidth-W)/2, kScreenHeight,W,H);
        _maskView.alpha = 0;
    } completion:^(BOOL finished) {
        _maskView.hidden = YES;
        [_maskView removeFromSuperview];
        [_subView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

@end
